package com.resource.provider.util;

import org.apache.log4j.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @author wangdekun
 * @ClassName: MD5
 * @Description: MD5加密
 * @date 2017年8月21日 上午10:14:51
 */
public class Md5Util {
    private static Logger logger = Logger.getLogger(Md5Util.class.getName());

    /**
     * @param plainText
     * @return 32位MD5小写密文
     */
    public static String md5(String plainText) {

        if (plainText == null) {
            plainText = "";
        }
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            re_md5 = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            logger.error("md5 加密Exception");
        }
        return re_md5;
    }


    public static String signatureMd5(String timestamp) {
        return md5(PropsUtil.getNoE("appID") + timestamp + PropsUtil.getNoE("token"));
    }


}
